package lxt.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import lxt.action.core.ActionSupport;
import lxt.entity.ClientInfo;
import lxt.service.ClientService;
import lxt.service.QueryDataService;

/**
 * 数据查询action
 * 
 * @author 王夫东
 * 
 */
public class QueryDataAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;

	/**
	 * 定义Service接口
	 */
	private QueryDataService queryDataService;
	private ClientService clientService;

	/**
	 * 定义成员变量
	 */
	private String clientId;
	private String clientName;
	private String clientType;
	private String linkMan;
	private String orderId;
	private String startDate;
	private String endDate;
	private ClientInfo clientInfo;
	private String truckId;
	private String truckName;
	private String driverName;
	/**
	 * 定义分页变量
	 */
	private Integer pageCounts = 10; // 每页显示数量
	private String currPageStr = "1"; // 分页
	private Integer totalCounts;// 总数据量

	/**
	 * 定义返回结果变量
	 */
	private List<Map<String, Object>> list;

	public QueryDataAction(){
		super();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		this.endDate = sdf.format(ca.getTime());
		ca.add(Calendar.DAY_OF_YEAR, -30);
		this.startDate = sdf.format(ca.getTime());
	}
	
	/**
	 * 欠费查询
	 * 
	 * @return
	 */
	@SuppressWarnings("finally")
	public String loadOwe() {
		try {
			int offset = (Integer.parseInt(currPageStr) - 1) * pageCounts;
			offset = offset < 0 ? 0 : offset;
			list = queryDataService.queryOweForPage(clientName, orderId,
					startDate, endDate, offset, pageCounts);
			totalCounts = queryDataService.queryOweForPage(clientName, orderId,
					startDate, endDate, null, null).size();
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		} finally {
			return SUCCESS;
		}
	}

	@SuppressWarnings("finally")
	public String loadOweDetail() {
		try {
			list = queryDataService.queryOweDetail(clientId, orderId);
			if (list != null && list.size() > 0) {
				clientType = (String) list.get(0).get("clientType");
				clientName = (String) list.get(0).get(clientType + "ClientName");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		} finally {
			return SUCCESS;
		}
	}

	@SuppressWarnings("finally")
	public String loadHistory() {
		try {
			list = queryDataService.queryChargeHistory(clientId, orderId);
			clientInfo = clientService.loadClientById(clientId);
			System.out.println(clientInfo == null);
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		} finally {
			return SUCCESS;
		}
	}

	/**
	 * 根据发货方查询
	 * @return
	 */
	@SuppressWarnings("finally")
	public String loadFeeBySendClient(){
		try {
			int offset = (Integer.parseInt(currPageStr) - 1) * pageCounts;
			offset = offset < 0 ? 0 : offset;
			list = queryDataService.loadFeeBySendClient(clientName, linkMan, startDate, endDate, offset, pageCounts);
			//应为返回结果包含统计行，故计算总条数需减1
			totalCounts = queryDataService.loadFeeBySendClient(clientName, linkMan, startDate, endDate, null, null).size() - 1;
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		} finally {
			return SUCCESS;
		}
	}
	/**
	 * 根据车辆查询费用
	 * @return
	 */
	@SuppressWarnings("finally")
	public String loadFeeByTruck(){
		try {
			int offset = (Integer.parseInt(currPageStr) - 1) * pageCounts;
			offset = offset < 0 ? 0 : offset;
			list = queryDataService.loadFeeByTruck(truckId, truckName, startDate, endDate, offset, pageCounts);
			totalCounts = queryDataService.loadFeeByTruck(truckId, truckName, startDate, endDate, null, null).size();
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		} finally {
			return SUCCESS;
		}
	}
	
	/**
	 * 根据驾驶员查询费用
	 * @return
	 */
	@SuppressWarnings("finally")
	public String loadFeeByDriver(){
		try {
			int offset = (Integer.parseInt(currPageStr) - 1) * pageCounts;
			offset = offset < 0 ? 0 : offset;
			list = queryDataService.loadFeeByDriver(driverName,  startDate, endDate, offset, pageCounts);
			totalCounts = queryDataService.loadFeeByDriver(driverName, startDate, endDate, null, null).size();
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		} finally {
			return SUCCESS;
		}
	}
	
	public void setQueryDataService(QueryDataService queryDataService) {
		this.queryDataService = queryDataService;
	}

	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public String getLinkMan() {
		return linkMan;
	}

	public void setLinkMan(String linkMan) {
		this.linkMan = linkMan;
	}

	public String getClientType() {
		return clientType;
	}

	public ClientInfo getClientInfo() {
		return clientInfo;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getTruckId() {
		return truckId;
	}

	public void setTruckId(String truckId) {
		this.truckId = truckId;
	}

	public String getTruckName() {
		return truckName;
	}

	public void setTruckName(String truckName) {
		this.truckName = truckName;
	}

	public String getDriverName() {
		return driverName;
	}

	public void setDriverName(String driverName) {
		this.driverName = driverName;
	}

	public List<Map<String, Object>> getList() {
		return list;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public Integer getPageCounts() {
		return pageCounts;
	}

	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}

	public String getCurrPageStr() {
		return currPageStr;
	}

	public void setCurrPageStr(String currPageStr) {
		this.currPageStr = currPageStr;
	}

	public Integer getTotalCounts() {
		return totalCounts;
	}

	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
}
